package com.qf.service.impl;/*
 *徐韬
 *2021/11/2
 *17:40
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.cache.filterCache;
import com.qf.constons.resultCode;
import com.qf.dto.R;
import com.qf.events.updateFilterevents;
import com.qf.exceptions.addDataException;
import com.qf.exceptions.queryDataException;
import com.qf.exceptions.updateException;
import com.qf.mapper.filterMapper;
import com.qf.pojo.MyAuthUser;
import com.qf.pojo.TFilters;
import com.qf.pojo.checkType;
import com.qf.service.filterService;
import com.sun.xml.internal.bind.v2.model.core.ReferencePropertyInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Service
@Transactional
public class filterServiceImpl implements filterService {
    private filterCache filterCache;
    private filterMapper filterMapper;
    private ApplicationContext context;
@Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Autowired
    public void setFilterMapper(com.qf.mapper.filterMapper filterMapper) {
        this.filterMapper = filterMapper;
    }

    @Autowired
    public void setFilterCache(com.qf.cache.filterCache filterCache) {
        this.filterCache = filterCache;
    }

    @Override
    public PageInfo<TFilters> showAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TFilters> tFilters = filterCache.getfinlAll();
        MyAuthUser authUser = (MyAuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(authUser.getType() != 1){
            throw new queryDataException("你没有权限查看该条数据", resultCode.NO_ROOT);
        }
        PageInfo<TFilters> pageInfo = new PageInfo<>(tFilters);
        return pageInfo;

    }

    @Override
    public void addFilter(TFilters tFilters) {
    if(tFilters.isNull(checkType.ADD)){
        throw new addDataException("您传入的数据有的值为空",resultCode.DATA_ISNULL);
    }
        int i = filterMapper.insertFilter(tFilters);

        context.publishEvent(new updateFilterevents());
    }

    @Override
    public void update(TFilters tFilters) {
        MyAuthUser authUser = (MyAuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
       if(authUser.getType()!=1){
           throw new updateException("您没有权限修改该条数据", resultCode.NO_ROOT);
       }
        if(tFilters.isNull(checkType.UPDATE)){
            throw new updateException("你没有权限修改该条数据",resultCode.NO_ROOT);
        }
        filterMapper.update(tFilters);
        context.publishEvent(new updateFilterevents());
    }

    @Override
    public void deleteByids(List<Integer> ids) {
        if(ids == null){
            throw new updateException("您传入的id为空", resultCode.DATA_ISNULL);
        }
        MyAuthUser authentication = (MyAuthUser) SecurityContextHolder.getContext().getAuthentication();
        Long type = authentication.getType();
        if(type != null){
            throw new updateException("ni你没有权限删除该条数据", resultCode.NO_ROOT);
        }
        filterMapper.delete(ids);
        context.publishEvent(new updateFilterevents());
    }


}
